/**
 * Created by chourongqishi on 2017/1/2.
 */
module.exports = function ($http, $q, httpservice,$cookies) {
    var settingService = {
      queryUserInfo: queryUserInfo,
      uploadPhoto: uploadPhoto,
      updateUserName: updateUserName,
      updateProfessionInfo: updateProfessionInfo,
      queryLawFirms: queryLawFirms,
      updateUserPassword: updateUserPassword,
      getBindEmail: getBindEmail,
      getBindPhone: getBindPhone,
      saveBindEmail: saveBindEmail,
      changeBindEmail: changeBindEmail,
      saveBindPhone: saveBindPhone,
      sendMesCod: sendMesCod,
      updateBindPhone: updateBindPhone,
      isOpenSendMessage: isOpenSendMessage,
      changeSendMessage: changeSendMessage,
      checkCellPhoneExist: checkCellPhoneExist,
      getEmailVerifyCode: getEmailVerifyCode,
      loginInfor: loginInfor,
      saveAttesInfo: saveAttesInfo,
      findAttesInfo: findAttesInfo, //查询实名认证基本信息
      findSkilled: findSkilled, //擅长领域列表
      setUserSkilled: setUserSkilled, //添加修改擅长领域列表
      findUserSkilled: findUserSkilled, //查询擅长领域列表
      deleteUserSkilled: deleteUserSkilled, //删除擅长领域列表
      updateUserInfo: updateUserInfo, //修改用户基本信息
      findDegreeList: findDegreeList, //获取学位列表
      findProvinceList: findProvinceList, //获取省份列表
      updateProfessionInfoNew: updateProfessionInfoNew, //修改执业信息
    };

    var userId = kbt.decrypt($cookies.get('cookieUserId'));

    // 查询用户信息
    function queryUserInfo() {

        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.queryUserInfoUrl,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 上传用户头像
    function uploadPhoto(fileData) {
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.uploadPhotoUrl,
            data:fileData,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    // 更新用户名
    function updateUserName(userName) {

        var params = {"id": userId, "userName": userName};

        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateUserNameUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 修改职业信息
    function updateProfessionInfo(professionType, professionNumber, companyId, entryDate) {
        var params = {
            "id": userId,
            "profession": professionType,
            "lawfirmid": companyId,
            "entryyear": entryDate,
            "certificateno": professionNumber
        };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateProfessionInfoUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 获取律所列表
     * @returns {*}
     */
    function queryLawFirms(name) {
      var params = {
        "name": name
      };
        var deferred = $q.defer();
        $http({
            method: 'GET',
            params:params,
            url: httpservice.lawfirmsUrl,
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 修改密码（知道原密码）
     * @param oldPassWord 原密码
     * @param newPassWord 新密码
     * @param affirmPassWord 确认密码
     * @returns {Promise}
     */
    function updateUserPassword(oldPassWord,newPassWord,affirmPassWord) {

        var params = {"id": userId, "oldPassWord": oldPassWord,"newPassWord":newPassWord,"affirmPassWord":affirmPassWord};

        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updatePasswordUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    /**
     * 查询是否绑定过邮箱
     * @returns {Promise}
     */
    function getBindEmail() {

        var params = {"id": userId};

        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getBindEmailUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    /**
     * 查询是否绑定过手机
     * @returns {Promise}
     */
    function getBindPhone() {

        var params = {"id": userId};

        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getBindPhoneUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 绑定邮箱
     * @returns {Promise}
     */
    function saveBindEmail(email) {

        var params = {"id": userId,"email":email};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.saveBindEmailUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 修改邮箱
     * @returns {Promise}
     */
    function changeBindEmail(oldEmail,emailValidateCode) {

        var params = {"email":oldEmail,"code":emailValidateCode};

        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.changeBindEmailUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //邮箱获取验证码
    function getEmailVerifyCode(params) {
      $http({
        method:"PUT",
        url:httpservice.getEmailVerifyCodeUrl,
        data : params,
        dataType : 'json',
        headers : {'Content-Type':'application/json'}
      })
    }


    /**
     * 绑定手机
     * @returns {Promise}
     */
    function saveBindPhone(cellPhone,validateCode) {

        var params = {"id": userId,"cellPhone":cellPhone,"validateCode":validateCode};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.saveBindPhoneUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //手机找回验证码
    function sendMesCod(newPhoneNum,oldPhoneNum,mode) {
        var  params ={"cellphone":newPhoneNum,'oldPhone':oldPhoneNum,"mode":mode};
        return $http({
            url : httpservice.getVerifyCodeUrl,
            method : 'POST',
            data : params,
            dataType : 'json',
            headers : {'Content-Type':'application/json'}
        })
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('getVerifyCode error', response.status, response.data);
            })
            .finally(function () {

            });
    };

    /**
     * 修改手机
     * @returns {Promise}
     */
    function updateBindPhone(oldCellPhone,newCellPhone,validateCode) {

        var params = {"id": userId,"oldCellPhone":oldCellPhone,"newCellPhone":newCellPhone,"validateCode":validateCode};

        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.changeBindPhoneUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //获取消息设置信息
    function isOpenSendMessage() {

        var params = {"userId": userId};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.isOpenSendMessageUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    }

    //设置信息是否发送
    function changeSendMessage(notificationId,status) {

        var params = {"userId": userId,"notificationId":notificationId,"status":status};

        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.changeSendMessageUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    }

    //检查手机号是否已使用
    function checkCellPhoneExist(cellphone) {
      return $http({
        url: httpservice.checkCellPhoneExistUrl,
        method: "POST",
        params: {cellPhone: cellphone}
      })
    }

    //登录信息
    function loginInfor(params) {
      return $http({
        url:httpservice.loginInforUrl,
        method:"POST",
        params:params
      })
    }
    
    //获取认证过的基本信息
    function findAttesInfo() {
        return $http({
            url: httpservice.findAttesUrl,
            method: "POST"
        })
    };
    
    // 提交认证信息
    function saveAttesInfo(fileData) {
        
        return false;
    }
    
    //获取擅长领域标签
    function findSkilled() {
        return $http({
            url: httpservice.findSkilledUrl,
            method: "POST"
        })
    }
    
    //保存擅长领域信息
    function setUserSkilled(params) {
        return $http({
            url:httpservice.setUserSkilledUrl,
            method:"POST",
            params:params
        })
    }
    //查询擅长领域
    function findUserSkilled() {
        return $http({
            url: httpservice.findUserSkilledUrl,
            method: "POST"
        })
    }
    //删除上传领域
    function deleteUserSkilled(params) {
        return $http({
            url: httpservice.deleteUserSkilledUrl,
            method: "POST",
            params:params
        })
    }
    
    //完善用户基本信息 
    function updateUserInfo(params) {
        return $http({
            url: httpservice.updateUserBaseInfoUrl,
            method: "PUT",
            data:params,
            headers: {'Content-Type': 'application/json'}
        })
    }
    
    //获取学位列表
    function findDegreeList() {
        return $http({
            url: httpservice.findDegreeListUrl,
            method: "POST"
        })
    }
    
    //获取省份列表
    function findProvinceList() {
        return $http({
            url: httpservice.findProvinceListUrl,
            method: "POST"
        })
    }
    
    //修改执业信息 原先的保留业务不一样
    function updateProfessionInfoNew(params) {
        return $http({
            url: httpservice.updateProfessionInfoUrl,
            method: "PUT",
            data:params,
            headers: {'Content-Type': 'application/json'}
        })
    }
    
    
    return settingService;
};
